<template>
  <div class="layout-container">
    <div class="layout-container-form" style="flex-direction: column">
      <div class="layout-container-opt">
        <el-row>
          <el-col :span="24" >
            <el-button
              type="primary"
              @click="handleAdd"
              v-if="adminInfo.admin_type == 0"
            >
              新建账号
            </el-button>
          </el-col>
        </el-row>
      </div>
    </div>
    <div class="layout-container-table">
      <Table
        ref="table"
        v-model:page="page"
        v-loading="loading"
        :showSelection="false"
        :data="tableData"
        @getTableData="getTableData"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="Id" label="ID" width="50"/>
        <el-table-column prop="user_name" label="管理员账号" align="center"/>
        <el-table-column prop="nick_name" label="管理员昵称"/>
        <!-- <el-table-column prop="nick_name" label="密码" align="center" width="100" >
          <template #default="scope">
            ********
          </template>
        </el-table-column> -->
        <el-table-column prop="name" label="管理员类型">
          <template #default="scope">
            <div v-if="scope.row.admin_type == 0">系统管理员</div>
            <div v-if="scope.row.admin_type == 1">副管理员</div>
            <div v-if="scope.row.admin_type == 2">子管理员</div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <div v-if="scope.row.status == 0">
              封禁
            </div>
            <div v-else>
              正常
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="project_permissions" label="项目权限">
          <template #default="scope">
            <span v-if="scope.row.admin_type == 0 || scope.row.admin_type == 1">全部</span>
            <span v-else v-for="(item) in scope.row.project_permissions" :key="item">
              {{item}}；
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="team_permissions" label="团队权限">
          <template #default="scope">
            <span v-if="scope.row.admin_type == 0 || scope.row.admin_type == 1">全部</span>
            <span v-else v-for="(item) in scope.row.team_permissions" :key="item">
              {{item}}；
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="action_permissions" label="操作权限">
          <template #default="scope">
            <div v-if="scope.row.admin_type == 0 || scope.row.admin_type == 1">全部</div>
            <div v-else>
              <span v-if="scope.row.action_permissions.includes('1')">查看权限；</span>
              <span v-if="scope.row.action_permissions.includes('2')">上传权限；</span>
              <span v-if="scope.row.action_permissions.includes('3')">下载权限；</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('message.common.handle')"
          width="180"
        >
          <template #default="scope">
              <div v-if="scope.row.admin_type > adminInfo.admin_type">
                <span>
                  <el-button type="text" @click="handleOpt(scope.row, 'edit')">
                  {{ $t("message.common.edit") }}
                  </el-button>
                </span>
                <span v-if="scope.row.status === 0">
                  <span class="split-line">|</span>
                  <el-button type="text" @click="handleOpt(scope.row, 'lift')">
                    {{ $t("message.common.lift") }}
                  </el-button>
                </span>
                <span v-if="scope.row.status === 1">
                  <span class="split-line">|</span>
                  <el-button type="text" @click="handleOpt(scope.row, 'prohibit')">
                    {{ $t("message.common.prohibit") }}
                  </el-button>
                </span>
                <span>
                  <span class="split-line">|</span>
                  <el-button type="text" @click="handleOpt(scope.row, 'del')">
                    {{ $t("message.common.del") }}
                  </el-button>
                </span>
              </div>
              <div v-else>
                —
              </div>
          </template>
        </el-table-column>
      </Table>
    </div>
  </div>
</template>

<script setup lang="ts">
import CryptoJS from 'crypto-js'
import { defineComponent, ref, reactive } from "vue";
import { Page } from "@/components/table/type";
import { ElMessage, ElMessageBox } from "element-plus";
import Table from "@/components/table/index.vue";
import { getSystemAdminList, delSystemAdmin, changeAdminStatus } from "@/api/service/user.ts";
import { useRouter, useRoute } from 'vue-router';
import { useStore } from "vuex";
const router = useRouter();
const route = useRoute();
const { state, commit } = useStore();
const adminInfo = reactive(state.user.adminInfo);

// 存储搜索用的数据
const query = reactive({
  dateRange: [], // 上线时间
});

//新建
const handleAdd = () => {
  commit('user/editAdminInfoChange', {});
  router.push({
    path: '/backstagerpzzm/accountManagement/addAccess',
    query: {
      type: 1 // 1: 新增 2: 修改
    }
  })
}

// 分页参数, 供table使用
const page: Page = reactive({
  index: 1,
  size: 50,
  total: 0,
});
const loading = ref(false);
const tableData = ref([]);
// 获取表格数据
// params <init> Boolean ，默认为false，用于判断是否需要初始化分页
const getTableData = (init: Boolean) => {
  loading.value = true;
  if (init) {
    page.index = 1;
  }
  let params = {
    page: page.index,
    page_size: page.size,
  };
  getSystemAdminList(params)
    .then((res: any) => {
      let data = res.data.list;
      data.forEach((d: any) => {
        d.loading = false;
      });

      tableData.value = data;
      page.total = Number(res.data.count);
    })
    .catch(() => {
      tableData.value = [];
      page.index = 1;
      page.total = 0;
    })
    .finally(() => {
      loading.value = false;
    });
};


// 勾选数据
const handleSelectionChange = (data: any) => {

};
// 操作按钮点击事件 
const showArrowFlag = ref(false)
const showArrow = () => {
  showArrowFlag.value = !showArrowFlag.value 
}
// 编辑
const handleOpt = (row: any, type: String) => {
    ElMessageBox.confirm(
    `是否确认当前操作?`,
    '',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'success',
    }
  )
  .then(() => {
    // 编辑
    if (type === 'edit') {
      handleEdit(row)
    }
    // 解禁
    if (type === 'lift') {
      changeAdminStatusFun(row);
    }
    // 封禁
    if (type === 'prohibit') {
      changeAdminStatusFun(row);
    }
    // 删除
    if (type === 'del') {
      delSystemAdminFun(row.Id);
    }
  })
  .catch(() => {
    
  })
}
// 编辑
let editData = reactive({})
const handleEdit = (row: any) => {
  commit('user/editAdminInfoChange', row);
  router.push({
    path: '/accountManagement/addAccess',
    query: {
      type: 2 // 1: 新增 2: 修改
    }
  })
}
// 解禁与封禁
const changeAdminStatusFun = (row: any) => {
  let data = {
    id: row.Id,
    status: row.status ? 0 : 1,
  }
  changeAdminStatus(data).then((res: any) => {
    if (res.code == 200) {
      getTableData(false);
      let msg = row.status ? '封禁成功！' : '解禁成功！'
      ElMessage.success(msg);
    }
  })
}

// 删除函数
const delSystemAdminFun = (id: any) => {
  let data = {
    id
  };
  delSystemAdmin(data)
  	.then((res: any) => {
  		if (res.code == 200) {
  			getTableData(false);
  			ElMessage.success("删除成功！");
  		} else {
  			ElMessage.error("删除失败！");
  		}
  	})
  	.catch(() => false);
};


getTableData(true);
</script>

<style lang="scss" scoped>
.layout-container-form {
  position: relative;
}
.layout-container-form-search {
  width: 100%;
  display: inline-block !important;
  justify-content: flex-start !important;
  .search-btn{
    margin-right: 10px;;
  }
}
.notice-title{
  margin-bottom: 10px;
}
.notice-content{
  width:100%;
  min-height:50px;
  padding:10px;
  box-sizing: border-box;
  background: #ebebeb;
  border: 1px solid #ccc;
  border-radius: 2px;
  margin-bottom: 10px;
  color: #929292;
}

.layout-container-opt {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 5px;
  margin-top: 5px;
}
.split-line{
  margin: 0 8px;
  color: #ccc;
}

</style>
